#!/bin/bash
#
# Copyright © 2001-2024 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED.
#
# This software is available to you under a choice of one of two
# licenses.  You may choose to be licensed under the terms of the GNU
# General Public License (GPL) Version 2, available from the file
# COPYING in the main directory of this source tree, or the
# BSD license below:
#
#     Redistribution and use in source and binary forms, with or
#     without modification, are permitted provided that the following
#     conditions are met:
#
#      - Redistributions of source code must retain the above
#        copyright notice, this list of conditions and the following
#        disclaimer.
#
#      - Redistributions in binary form must reproduce the above
#        copyright notice, this list of conditions and the following
#        disclaimer in the documentation and/or other materials
#        provided with the distribution.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

# vma:         Start the VMA Daemon
#
# chkconfig:   345 80 20
# description: This is a daemon which handles the task of \
#              monitoring processes launched under VMA.
#
### BEGIN INIT INFO
# Provides: vma
# Required-Start: $local_fs $syslog
# Required-Stop: $local_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start the VMA Daemon
# Description: This is a daemon which handles the task of
#              monitoring processes launched under VMA.
### END INIT INFO

prefix=@prefix@
exec_prefix=@exec_prefix@
  
RETVAL=0
exefile=vmad
options=
pidfile=/var/run/lock/vmad.lock

# Source function library.
if [[ -s /etc/init.d/functions ]]; then
    # RHEL / CentOS / SL / Fedora.
    . /etc/init.d/functions
    rc_status() { :; }
elif [[ -s /lib/lsb/init-functions ]]; then
    # SLES / openSuSE / Debian.
    . /lib/lsb/init-functions
    success() { log_success_msg; }
    failure() { log_failure_msg; }
elif [[ -s /etc/rc.status ]]; then
    # Older SuSE systems.
    . /etc/rc.status
    failure() { rc_status -v; }
    success() { rc_status -v; }
fi


function check_running()
{
    [ -e ${pidfile} ] &&
        [ "$(readlink "/proc/$(<${pidfile})/exe")" = "@sbindir@/${exefile}" ]
}

function check_permission()
{
    [ "$(id -u)" = "0" ]
}

function do_start()
{
    echo -n "Starting ${exefile}: "

    if check_running; then
        RETVAL=0
    else
        @sbindir@/${exefile} ${options} > /dev/null 2>&1
        RETVAL=$?
    fi
    if [[ $RETVAL -eq 0 ]]; then
        success
        sleep 1
    else
        failure
    fi
    echo
}

function do_stop()
{
    echo -n "Shutting down ${exefile}: "

    RETVAL=0
    if [ $(command -v pkill >/dev/null 2>&1 && echo $?) ]; then
        pkill -INT "${exefile}"
        RETVAL=$?
    fi
    if [[ $RETVAL -ne 0 ]]; then
        killproc "${exefile}" -INT
        RETVAL=$?
    fi
    if [[ $RETVAL -eq 0 ]]; then
        success
        sleep 1
    else
        failure
    fi
    echo
}

function do_status ()
{
    pid="`pidof ${exefile}`"
    ret=$?
    if [ $ret -eq 0 ] ; then
        echo "${exefile} is running... pid=$pid"
    else
        echo "${exefile} is not running."
    fi
}

function do_restart()
{
    do_stop
    do_start
}


function do_check()
{
    if ! check_permission; then
        echo "root permission is required"
        exit 1
    fi
}

case "$1" in
    start)
        do_check
        do_start
        ;;
    stop)
        do_check
        do_stop
        ;;
    status)
        do_status
        ;;
    restart | force-reload)
        do_check
        do_restart
        ;;
    *)
    echo $"Usage: $0 {start|stop|status|restart|force-reload}"
    RETVAL=1
    ;;
esac

exit $RETVAL
